<template>
  <el-dialog
    :title="!dataForm.vid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="章节ID" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="章节ID"></el-input>
    </el-form-item>
    <el-form-item label="视频长度" prop="videoLength">
      <el-input v-model="dataForm.videoLength" placeholder="视频长度"></el-input>
    </el-form-item>
    <el-form-item label="视频名称" prop="videoName">
      <el-input v-model="dataForm.videoName" placeholder="视频名称"></el-input>
    </el-form-item>
    <el-form-item label="视频状态" prop="videoStatus">
      <el-input v-model="dataForm.videoStatus" placeholder="视频状态"></el-input>
    </el-form-item>

<!--      <el-form-item label="视频上传" prop="videoAddress">-->
<!--&lt;!&ndash;        <el-input v-model="dataForm.videoAddress"  placeholder="视频存储地址"></el-input>&ndash;&gt;-->
<!--        <el-upload-->
<!--          class="avatar-uploader"-->
<!--          action="http://localhost:8081/backstage/video/upload"-->
<!--          name="multipartFile"-->
<!--          :show-file-list="true"-->
<!--          :on-success="handleAvatarSuccess"-->
<!--          :before-upload="beforeAvatarUpload">-->
<!--          <video v-if="this.dataForm.videoAddress" :src="this.dataForm.videoAddress" class="avatar" ></video>-->
<!--          <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
<!--        </el-upload>-->

<!--      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          vid: 0,
          videoAddress: '',
          cid: this.$route.query.cid,
          videoLength: '',
          videoName: '',
          videoStatus: ''
        },
        dataRule: {
          cid: [
            { required: true, message: '章节ID不能为空', trigger: 'blur' }
          ],
          videoLength: [
            { required: true, message: '视频长度不能为空', trigger: 'blur' }
          ],
          videoName: [
            { required: true, message: '视频名称不能为空', trigger: 'blur' }
          ],
          videoStatus: [
            { required: true, message: '视频状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.vid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.vid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/video/info/${this.dataForm.vid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.videoAddress = data.video.videoAddress
                this.dataForm.cid = data.video.cid
                this.dataForm.videoLength = data.video.videoLength
                this.dataForm.videoName = data.video.videoName
                this.dataForm.videoStatus = data.video.videoStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/video/${!this.dataForm.vid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'vid': this.dataForm.vid || undefined,
                'videoAddress': this.dataForm.videoAddress,
                'cid': this.dataForm.cid,
                'videoLength': this.dataForm.videoLength,
                'videoName': this.dataForm.videoName,
                'videoStatus': this.dataForm.videoStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleAvatarSuccess (res, file) {
        this.dataForm.videoAddress = res.data.url
        console.log(res.data.url)
      },
      beforeAvatarUpload () {
      }
    }
  }
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
